package com.parsechina.hpush.broker.spi;

import com.parsechina.hpush.broker.internal.MessageGUID;
import com.parsechina.hpush.broker.internal.StoredMessage;
import com.parsechina.hpush.broker.internal.subscriptions.Subscription;

import java.util.Collection;
import java.util.List;

/**
 * Defines the SPI to be implemented by a StorageService that handle persistence of messages
 */
public interface IMessagesStore {

    /**
     * Used to initialize all persistent store structures
     */
    void initStore();

    /**
     * 存储消息ID
     * 如果 guid 是空的，则清除指定topic的guid
     * @param topic 主题
     * @param guid ID
     */
    void storeRetained(String topic, MessageGUID guid);

    /**
     * 清除存储的消息ID
     * @param topic 主题
     */
    void cleanRetained(String topic);

    /**
     * Return a list of retained messages that satisfy the condition.
     */
    Collection<StoredMessage> searchMessage(Subscription condition);

    /**
     * Persist the message.
     *
     * @return the unique id in the storage (guid).
     */
    MessageGUID storePublishForFuture(StoredMessage evt);

    /**
     * Return the list of persisted publishes for the given clientId.
     * For QoS1 and QoS2 with clean session flag, this method return the list of
     * missed publish events while the client was disconnected.
     */
    List<StoredMessage> listMessagesInSession(Collection<MessageGUID> guids);

    void dropMessagesInSession(String clientID);

    void removeStoredMessage(MessageGUID guid);

    StoredMessage getMessageByGuid(MessageGUID guid);

    void incUsageCounter(MessageGUID guid);

    void decUsageCounter(MessageGUID guid);
}
